/*   1:    */ package com.atomikos.persistence.imp;
/*   2:    */ 
/*   3:    */ import com.atomikos.finitestates.FSMEnterEvent;
/*   4:    */ import com.atomikos.finitestates.FSMPreEnterListener;
/*   5:    */ import com.atomikos.persistence.LogException;
/*   6:    */ import com.atomikos.persistence.ObjectImage;
/*   7:    */ import com.atomikos.persistence.StateRecoverable;
/*   8:    */ import com.atomikos.persistence.StateRecoveryManager;
/*   9:    */ import java.util.HashMap;
/*  10:    */ import java.util.Iterator;
/*  11:    */ import java.util.Map;
/*  12:    */ import java.util.Set;
/*  13:    */ import java.util.Vector;
/*  14:    */ 
/*  15:    */ public class VolatileStateRecoveryManager
/*  16:    */   implements StateRecoveryManager, FSMPreEnterListener
/*  17:    */ {
/*  18:    */   private Map idToElementMap;
/*  19:    */   
/*  20:    */   public VolatileStateRecoveryManager()
/*  21:    */   {
/*  22: 63 */     this.idToElementMap = new HashMap();
/*  23:    */   }
/*  24:    */   
/*  25:    */   public void init()
/*  26:    */     throws LogException
/*  27:    */   {}
/*  28:    */   
/*  29:    */   public void register(StateRecoverable staterecoverable)
/*  30:    */   {
/*  31: 81 */     if (staterecoverable == null) {
/*  32: 82 */       throw new IllegalArgumentException("null in register arg");
/*  33:    */     }
/*  34: 83 */     Object[] states = staterecoverable.getRecoverableStates();
/*  35: 84 */     if (states != null)
/*  36:    */     {
/*  37: 85 */       for (int i = 0; i < states.length; i++) {
/*  38: 86 */         staterecoverable.addFSMPreEnterListener(this, states[i]);
/*  39:    */       }
/*  40: 88 */       states = staterecoverable.getFinalStates();
/*  41: 89 */       for (int i = 0; i < states.length; i++) {
/*  42: 90 */         staterecoverable.addFSMPreEnterListener(this, states[i]);
/*  43:    */       }
/*  44:    */     }
/*  45:    */   }
/*  46:    */   
/*  47:    */   public synchronized void preEnter(FSMEnterEvent event)
/*  48:    */     throws IllegalStateException
/*  49:    */   {
/*  50:102 */     Object state = event.getState();
/*  51:103 */     StateRecoverable source = (StateRecoverable)event.getSource();
/*  52:104 */     ObjectImage img = source.getObjectImage(state);
/*  53:106 */     if (img != null)
/*  54:    */     {
/*  55:107 */       StateObjectImage simg = new StateObjectImage(img);
/*  56:108 */       Object[] finalstates = source.getFinalStates();
/*  57:109 */       boolean delete = false;
/*  58:111 */       for (int i = 0; i < finalstates.length; i++) {
/*  59:112 */         if (state.equals(finalstates[i])) {
/*  60:113 */           delete = true;
/*  61:    */         }
/*  62:    */       }
/*  63:116 */       if (!delete) {
/*  64:117 */         this.idToElementMap.put(simg.getId(), simg);
/*  65:    */       } else {
/*  66:119 */         this.idToElementMap.remove(simg.getId());
/*  67:    */       }
/*  68:    */     }
/*  69:    */   }
/*  70:    */   
/*  71:    */   public void close()
/*  72:    */     throws LogException
/*  73:    */   {}
/*  74:    */   
/*  75:    */   public StateRecoverable recover(Object id)
/*  76:    */     throws LogException
/*  77:    */   {
/*  78:139 */     StateRecoverable ret = null;
/*  79:    */     
/*  80:141 */     StateObjectImage simg = (StateObjectImage)this.idToElementMap.get(id);
/*  81:142 */     ret = (StateRecoverable)simg.restore();
/*  82:    */     
/*  83:144 */     return ret;
/*  84:    */   }
/*  85:    */   
/*  86:    */   public Vector recover()
/*  87:    */     throws LogException
/*  88:    */   {
/*  89:153 */     Vector ret = new Vector();
/*  90:154 */     Iterator keys = this.idToElementMap.keySet().iterator();
/*  91:155 */     while (keys.hasNext())
/*  92:    */     {
/*  93:156 */       Object key = keys.next();
/*  94:157 */       StateObjectImage simg = (StateObjectImage)this.idToElementMap.get(key);
/*  95:    */       
/*  96:159 */       ret.add(simg.restore());
/*  97:    */     }
/*  98:161 */     return ret;
/*  99:    */   }
/* 100:    */   
/* 101:    */   public synchronized void delete(Object id)
/* 102:    */     throws LogException
/* 103:    */   {
/* 104:170 */     this.idToElementMap.remove(id);
/* 105:    */   }
/* 106:    */ }


/* Location:           C:\myfile\mysoft\apache-maven-3.5.0-bin\apache-maven-3.5.0\myrep\com\atomikos\transactions\3.7.0\transactions-3.7.0.jar
 * Qualified Name:     com.atomikos.persistence.imp.VolatileStateRecoveryManager
 * JD-Core Version:    0.7.0.1
 */